import {defineStore} from "pinia";
import {ref} from "vue";


export const useMenuStore = defineStore('menu', () => {
    const menus = ref([])
    const active = ref('')

    const load = (v = []) => {
        menus.value = v
    }

    const activeMenu = (idx = '') => {
        active.value = idx
    }

    const getItemRouter = (name = '') => {
        for (let i = 0; i < menus.value.length; i++) {
            if (menus.value[i].router === name) {
                return menus.value[i]
            }
            for (let j = 0; j < menus.value[i].children.length; j++) {
                if (menus.value[i].children[j].router === name) {
                    return menus.value[i].children[j]
                }
            }
        }
        return null
    }

    const getItem = (idx = '') => {
        for (let i = 0; i < menus.value.length; i++) {
            if (menus.value[i].index === idx) {

                return menus.value[i]
            }
            for (let j = 0; j < menus.value[i].children.length; j++) {
                if (menus.value[i].children[j].index === idx) {
                    return menus.value[i].children[j]
                }
            }
        }

    }

    return {menus, load, getItem, activeMenu, getItemRouter, active}
})